package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysJobLogDO;
import com.andy.blog.dal.mapper.SysJobLogMapper;
import com.andy.blog.dal.service.SysJobLogDalService;
import com.andy.blog.dal.vo.SysJobLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by andy on 2023/5/12 21:59
 */
@Service
public class SysJobLogDalServiceImpl implements SysJobLogDalService {

    @Resource
    private SysJobLogMapper sysJobLogMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SysJobLogDO> buildQueryWrapper(SysJobLogVO sysJobLogVO) {
        LambdaQueryWrapper<SysJobLogDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper
                .like(StringUtils.isNotBlank(sysJobLogVO.getJobName()), SysJobLogDO::getJobName, sysJobLogVO.getJobName())
                .eq(StringUtils.isNotBlank(sysJobLogVO.getJobGroup()), SysJobLogDO::getJobGroup, sysJobLogVO.getJobGroup())
                .eq(StringUtils.isNotBlank(sysJobLogVO.getStatus()), SysJobLogDO::getStatus, sysJobLogVO.getStatus())
                .like(StringUtils.isNotBlank(sysJobLogVO.getInvokeTarget()), SysJobLogDO::getInvokeTarget, sysJobLogVO.getInvokeTarget())
                .ge(StringUtils.isNotBlank(sysJobLogVO.getBeginTime()), SysJobLogDO::getCreateTime, sysJobLogVO.getCreateTime())
                .le(StringUtils.isNotBlank(sysJobLogVO.getEndTime()), SysJobLogDO::getCreateTime, sysJobLogVO.getCreateTime())
                .orderByDesc(SysJobLogDO::getId);

        return lambdaQueryWrapper;
    }


    @Override
    public IPage<SysJobLogDO> selectListByPage(SysJobLogVO sysJobLogVO) {
        LambdaQueryWrapper<SysJobLogDO> lambdaQueryWrapper = buildQueryWrapper(sysJobLogVO);

        IPage<SysJobLogDO> page = new Page<>(sysJobLogVO.getPageNum(), sysJobLogVO.getPageSize());
        return sysJobLogMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<SysJobLogDO> selectListByCondition(SysJobLogVO sysJobLogVO) {
        LambdaQueryWrapper<SysJobLogDO> lambdaQueryWrapper = buildQueryWrapper(sysJobLogVO);

        return sysJobLogMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public SysJobLogDO selectById(Long id) {
        return sysJobLogMapper.selectById(id);
    }

    @Override
    public int deleteByIds(List<Long> logIds) {
        return sysJobLogMapper.deleteBatchIds(logIds);
    }

    @Override
    public void cleanJobLog() {
        sysJobLogMapper.cleanJobLog();
    }

    @Override
    public void insertJobLog(SysJobLogVO jobLogVO) {
        SysJobLogDO transform = TransformUtils.transform(jobLogVO, SysJobLogDO.class);

        try {
            sysJobLogMapper.insert(transform);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }

    @Override
    public int deleteById(Long jobId) {
        return sysJobLogMapper.deleteById(jobId);
    }
}
